package pers.lonestar.framework.utils.support.converters;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;

import pers.lonestar.framework.utils.support.enums.CellDataTypeEnum;
import pers.lonestar.framework.utils.support.metadata.CellData;

public class CalendarConverter implements Converter<Calendar> {
    @Override
    public Calendar convertToJavaObject(CellData<Calendar> cellData) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date((long) ((cellData.getNumberValue().doubleValue() - 25567) * 86400000)));
        return calendar;
    }

    @Override
    public CellData<Calendar> convertToCellData(Calendar calendar) {
        CellData<Calendar> cellData = new CellData<>();
        cellData.setNumberValue(BigDecimal.valueOf(calendar.getTimeInMillis() / 86400000 + 25567));
        cellData.setType(CellDataTypeEnum.DATE);
        cellData.setData(calendar);
        return cellData;
    }
}
